//
//  BCStuffVC.m
//  BangcaiSale
//
//  Created by lsy on 16/7/7.
//  Copyright © 2016年 Bangcai. All rights reserved.
//

#import "BCStuffVC.h"
#import "WKPageView.h"
#import "BCStuffFootView.h"

#import "BCsellingView.h"
#import "BCSoldOutView.h"
#import "BCUpStuffVC.h"

@interface BCStuffVC ()<WKPageViewDataSource,WKPageViewDelegate,clickDelegate>
{
    UIView *headView;
    UIImageView* headImg;
    UIButton *headLeftButton;
    UIButton *headRightButton;
}
@property(nonatomic, strong)UIScrollView *stuffScroll;
@property(nonatomic, strong)BCsellingView *sellView;
@property(nonatomic, strong)WKPageView *soldView;
@property(nonatomic, assign)NSInteger page;

@property (nonatomic, strong) NSArray *items;
@property (nonatomic, strong)BCStuffFootView *footView;


@property (nonatomic, strong)NSArray *dataArr;                      //数据数组
@property (nonatomic, strong)NSMutableArray *amoutArr;              //是否被选中数组
@end

@implementation BCStuffVC

- (instancetype)initWithIndex:(NSIndexPath *)index
{
    self = [super init];
    if (self) {
        self.ClickIndex = index;
        [self getValue];
        [self getView];
    }
    return self;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)getValue{
    
    self.items = [NSArray arrayWithObjects:@"禽蛋肉品",@"蔬菜水果", nil];
    NSArray *arr0 = @[@"全部",
                      @"猪肉",
                      @"牛肉",
                      @"羊肉",
                      @"禽类",
                      @"猪肉冻",
                      @"禽类冻",
                      @"牛羊肉冻",
                      @"蛋类",
                      @"活禽",
                      @"调理肉类"];
    NSArray *arr1 = @[@"全部",
                      @"叶菜类",
                      @"茄果类",
                      @"根茎类",
                      @"豆类",
                      @"特菜",
                      @"菌类",
                      @"葱姜蒜",
                      @"水果"];
    self.dataArr = [NSArray arrayWithObjects:arr0, arr1, nil];
    NSMutableArray *aarr0 = [NSMutableArray array];
    NSMutableArray *aarr1 = [NSMutableArray array];
    self.amoutArr = [NSMutableArray array];
    [self.amoutArr addObject:aarr0];
    [self.amoutArr addObject:aarr1];


}

- (void)getView{
    self.edgesForExtendedLayout = UIRectEdgeNone;

    headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    headView.backgroundColor = [UIColor colorWithRed:0.205  green:0.285  blue:0.368 alpha:1];
    headImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"左白"]];
    headImg.userInteractionEnabled = YES;
    headLeftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    headRightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [headLeftButton setTintColor:[UIColor colorWithRed:0.205  green:0.285  blue:0.368 alpha:1]];
    [headRightButton setTintColor:[UIColor whiteColor]];
    [headLeftButton setTitle:@"出售中" forState:UIControlStateNormal];
    [headRightButton setTitle:@"已下架" forState:UIControlStateNormal];
    
    [headRightButton addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside];
    [headLeftButton addTarget:self action:@selector(leftClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:headView];
    [headView addSubview:headImg];
    [headImg addSubview:headLeftButton];
    [headImg addSubview:headRightButton];
    
    [headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(-10);
        make.height.mas_equalTo(40);
        make.height.mas_equalTo(28);
        make.width.mas_equalTo(230 *wx);
    }];
    [headLeftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(headImg.mas_centerX);
        make.top.and.bottom.mas_equalTo(0);
        
    }];
    
    [headRightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.left.mas_equalTo(headImg.mas_centerX);
        make.top.and.bottom.mas_equalTo(0);
    }];
    
    self.stuffScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT- 64)];
    self.stuffScroll.contentSize = CGSizeMake(SCREEN_WIDTH *2, SCREEN_HEIGHT- 114);
    [self.view addSubview:self.stuffScroll];
    self.sellView = [[BCsellingView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-114)];
    [self.sellView.logoImg addTarget:self action:@selector(goChoose) forControlEvents:UIControlEventTouchUpInside];

    
    self.soldView = [[WKPageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 114)];
        self.soldView.dataSource = self;
    self.soldView.delegate = self;
    self.soldView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.soldView];
    self.footView = [[BCStuffFootView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50)];
    self.footView.hidden = YES;
    [self.footView.coomit setTitle:@"重新上架" forState:UIControlStateNormal];
    self.footView.logoImg = [UIImage imageNamed:@"购物车1"];
    [self.view addSubview:self.footView];
    [self.footView.coomit addTarget:self action:@selector(commit:) forControlEvents:UIControlEventTouchUpInside];

    
    
    self.stuffScroll.pagingEnabled = YES;
    self.stuffScroll.scrollEnabled = NO;
    self.stuffScroll.showsHorizontalScrollIndicator = NO;
    [self.stuffScroll addSubview:self.sellView];
    [self.stuffScroll addSubview:self.soldView];
}

- (void)leftClick:(UIButton *)leftbut{
    if (self.rdv_tabBarController.tabBarHidden) {
        [self.rdv_tabBarController setTabBarHidden:NO];

    }
    self.footView.hidden = YES;

   [headImg setImage:[UIImage imageNamed:@"左白"]];
    [headLeftButton setTintColor:[UIColor colorWithRed:0.205  green:0.285  blue:0.368 alpha:1]];
    [headRightButton setTintColor:[UIColor whiteColor]];
    self.stuffScroll.contentOffset = CGPointMake(0, 0);

}
- (void)rightClick:(UIButton *)rightbut{
    
    [headImg setImage:[UIImage imageNamed:@"右白"]];
    [headLeftButton setTintColor:[UIColor whiteColor]];
    [headRightButton setTintColor:[UIColor colorWithRed:0.205  green:0.285  blue:0.368 alpha:1]];
    self.stuffScroll.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    if (!self.rdv_tabBarController.tabBarHidden) {
        [self.rdv_tabBarController setTabBarHidden:YES];
    }
    self.footView.hidden = NO;

}



- (void)goChoose{
    BCUpStuffVC *stufVC = [[BCUpStuffVC alloc] init];
    [self.navigationController pushViewController:stufVC animated:YES];

}

- (void)commit:(UIButton *)but
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark 协议事件
- (void)goSecond{
    NSLog(@"click Second");
}

- (void)addAmount{
    
    self.footView.amount +=1;
}

- (void)removeAmount
{
    self.footView.amount -=1;
    
}

- (void)getAmount:(NSMutableArray *)arr index:(NSInteger)index
{
    self.amoutArr[index] = arr;
}

- (NSArray *)menuItemsForMenuViewInPageView:(WKPageView *)pageView{
    return self.items;
}

- (WKPageCell *)pageView:(WKPageView *)pageView cellForIndex:(NSInteger)index{
    static NSString *identifier = @"pageCell";
    WKPageCell *cell = [pageView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[WKPageCell alloc] initWithIdentifier:identifier];
    }
    cell.backgroundColor = [UIColor colorWithRed:0.949  green:0.949  blue:0.949 alpha:1];
    cell.secondArr = self.dataArr[index];
    cell.chooseNumArr = self.amoutArr[index];
    cell.delegate = self;
    cell.index = index;
    cell.reddotAnimationBlock = ^(CAAnimation *anim) {
        if ([[anim valueForKey:@"animationName"]isEqualToString:@"groupsAnimation"]) {
            
            CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
            shakeAnimation.duration = 0.25f;
            shakeAnimation.fromValue = [NSNumber numberWithFloat:0.9];
            shakeAnimation.toValue = [NSNumber numberWithFloat:1];
            shakeAnimation.autoreverses = YES;
        }
    };
    return cell;
}

// 若不实现，默认红黑，为保证字体颜色渐变，请尽量选用RGBA来创建UIColor
- (UIColor *)titleColorOfMenuItemInPageView:(WKPageView *)pageView withState:(WKMenuItemTitleColorState)state{
    if (state == WKMenuItemTitleSizeStateNormal) {
        return [UIColor colorWithRed:0.2  green:0.2  blue:0.2 alpha:1];
    }else if (state == WKMenuItemTitleSizeStateSelected)
    {
        return [UIColor colorWithRed:0  green:0.454  blue:0.999 alpha:1];
    }else
    {
        return [UIColor redColor];
    }
}

// 若不实现,默认为30
- (CGFloat)pageView:(WKPageView *)pageView heightForMenuView:(WKMenuView *)menuView{
    return 50;
}
// 若不实现，默认为15/18
- (CGFloat)titleSizeOfMenuItemInPageView:(WKPageView *)pageView withState:(WKMenuItemTitleSizeState)state{
    switch (state) {
        case WKMenuItemTitleSizeStateNormal:
            return 14;
            break;
        case WKMenuItemTitleSizeStateSelected:
            return 14;
        default:
            break;
    }
}
// 若不实现，默认为灰色
- (UIColor *)backgroundColorOfMenuViewInPageView:(WKPageView *)pageView{
    return [UIColor whiteColor];
}
// MenuView内部各个item的宽度，若标题过长可自行设置，默认为60
- (CGFloat)pageView:(WKPageView *)pageView widthForMenuItemAtIndex:(NSInteger)index{
    return 80;
}




@end
